//
// This file is part of cpp-lib, a library of C++ functions and classes.
// Cpp-lib is copyright (C) 2004 and onwards, The CPL Interest Group.
// 
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this library (see the file COPYING); if not, write to the
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307  USA
//
// $Id$
//


#ifndef CPP_LIB_FILE_UTIL_H
#define CPP_LIB_FILE_UTIL_H

#include <string>
#include <vector>
#include <memory>
#include <istream>
#include <ostream>
#include <streambuf>

#include <cassert>

#include "boost/shared_ptr.hpp"


#include "cpp-lib/util.h"


// Forward declaration for file.
namespace cpl { namespace detail_ { struct file_impl ; } }


namespace cpl {
  namespace util {
    namespace file {



//
// owning_istream and owning_ostream are stream classes which 
//
// 1. Always have a buffer (DON'T call rdbuf( 0 )).
// 2. Delete their current buffer on destruction.
//

struct owning_istream : std::istream {

  owning_istream( std::auto_ptr< std::streambuf > buf )
  : std::istream( buf.release() ) 
  { assert( rdbuf() ) ; }

  ~owning_istream()
  { assert( rdbuf() ) ; delete rdbuf() ; }

} ;


struct owning_ostream : std::ostream {

  owning_ostream( std::auto_ptr< std::streambuf > buf )
  : std::ostream( buf.release() ) 
  { assert( rdbuf() ) ; }

  ~owning_ostream()
  { assert( rdbuf() ) ; delete rdbuf() ; }

} ;



    //
    // Open a file buffer for reading.
    //
    // Open the file.  If the operation fails, a std::runtime_error
    // containing an appropriate message is thrown.
    //
    // The function tries all path names path[ i ] + name starting from
    // i = 0 and finally tries name alone.
    //
    // After a successful open, which contains the full pathname of the
    // opened file.
    //

    std::auto_ptr< std::filebuf > open_readbuf( 
      std::string const& name  ,
      std::string      & which ,
      std::vector< std::string > const& path = std::vector< std::string >()
    ) ;
    

	//
	// Open a file by delegating to open_readbuf().
	//
	
	std::auto_ptr< std::istream > open_read( 
      std::string const& name  ,
      std::string      & which ,
      std::vector< std::string > const& path = std::vector< std::string >()
    ) ;


    //
    // Equivalent to the above function but without information about
    // the opened file.
    //

    inline std::auto_ptr< std::istream > open_read( 
      std::string const& name  ,
      std::vector< std::string > const& path = std::vector< std::string >()
    ) { std::string dummy ; return open_read( name , dummy , path ) ; }


    // Open a file buffer for writing.

    // Open the file.  If the operation fails, a std::runtime_error
    // containing an appropriate message is thrown.

    std::auto_ptr< std::filebuf > open_writebuf( std::string const& ) ;
    
	
	// Open a file for writing by delegating to open_writebuf().

    std::auto_ptr< std::ostream > open_write( std::string const& ) ;


    /// Strip suffix from filename.

    /// If \a name ends in \a suffix, return \a name with \a suffix removed.
    /// Otherwise, return \a name.

    std::string basename( std::string const& name , std::string const& suffix ) ;


    /// A class to redirect an ostream to a file.

    struct redirector {

      /// As long as the redirector exists, \a os is redirected to file \a name
      /// by switching the buffer.
      /// Uses cpl::util::open() for opening the file.

      redirector( std::ostream& os , std::string const& name ) :
        file( cpl::util::file::open_write( name ) ) , saved( os.rdbuf() ) , redirected( os )
      { os.rdbuf( file->rdbuf() ) ; }

      /// Restore the original buffer.

      ~redirector() { 
        always_assert( saved != 0 ) ; 
        redirected.rdbuf( saved ) ; 
      }

    private:

      /// The file to redirect to.

      std::auto_ptr< std::ostream > file ;

      /// The saved stream buffer.

      std::streambuf* saved ;

      /// The redirected stream.

      std::ostream& redirected ;

    } ;

    //@}



struct File {

  // Open the file.  Subsequent opens are possible during our lifetime.
  File( std::string const& name ) ;

  // Return last access time [s] (since some fixed epoch). 
  double const modification_time() ;

private:

  boost::shared_ptr< cpl::detail_::file_impl > impl ;

} ;


//
// An object watching the modification status of a file.
//
// (Necessary because Windows doesn't correctly report modification
// times once file is opened.)
//

struct file_name_watcher {

  file_name_watcher( std::string const& name ) 
  : name( name ) , t( File( name ).modification_time() ) {}

  // True iff the file was written to since last modified() call.
  bool modified() ;

private:

  std::string const name ;
  double t ;

} ;


//
// An object watching the modification status of a file.
//

struct file_watcher {

  file_watcher( std::string const& name ) 
  : f( name ) , t( f.modification_time() ) {}

  // True iff the file was written to since last modified() call.
  bool modified() ;

private:

  File f ;
  double t ;

} ;


} // namespace file

} // namespace util

} // namespace cpl



#endif // CPP_LIB_FILE_UTIL_H
